package org.ktm.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.ktm.authen.AuthenticatorFactory;
import org.ktm.utils.Globals;

public abstract class SecureServlet extends AbstractServlet {

	private static final long	serialVersionUID	= 1L;

	@Override
	protected boolean
			prepareRequest( HttpServletRequest request ) throws ServletException,
														IOException {
		super.prepareRequest( request );
		AuthenticatorFactory.saveRequestContext( request );
		return AuthenticatorFactory.isUserLoggedIn( request );
	}

	@Override
	protected ActionForward
			processRequest( HttpServletRequest request,
							HttpServletResponse response,
							final String htmlMethod,
							final String htmlModule )	throws ServletException,
														IOException,
														ClassNotFoundException,
														InstantiationException,
														IllegalAccessException {
		String loginUri = getServletContext( request ).getInitParameter( Globals.LOGIN_PAGE );
		ActionForward action = ActionForward.getUri( this, request, loginUri );
		if ( prepareRequest( request ) ) {
			action = processRequest( getBean( request ),
					request,
					response,
					htmlMethod,
					htmlModule );
		}
		return action;
	}
}
